#include "../include/structure.h"

void *thread_unlock(void *arg){
    pthread_mutex_t *lock = (pthread_mutex_t*)arg;
    pthread_mutex_unlock(lock);

    return NULL;
}


void* threadMain(void *arg){
    pool_t *pool = (pool_t*)arg;
    printf("线程，启动！\n");

    
    while(1){
        pthread_mutex_lock(&pool->lock);
        pthread_cleanup_push(thread_unlock, &pool->lock);
        while(pool->queue.size <= 0){
            // 取消点
            // 子线程有可能带锁死亡，别的线程就亖不了的
            pthread_cond_wait(&pool->cond, &pool->lock);
        }

        int net_fd = pool->queue.head->net_fd;

        deQueue(&pool->queue);
        // pthread_mutex_unlock(&pool->lock);
        

        printf("文件，发送！\n");
        // 通过net_fd给客户端传文件
        sendFile(net_fd);

        close(net_fd);
        pthread_cleanup_pop(1);
    }

    return (void*)0;
}